#include <iostream>
#include <vector>
#include <string>

using namespace std;

bool isSubsequence(string s, string t) {
    int i = 0, j= 0;
    while(i < s.size() && j < t.size()){
        if(s[i] == t[j]){
            i++;
        }
        j++;
    }
    return i == s.size();
}

string findLongestWord(string s, vector<string>& dictionary) {
    int i, j = -1, len = 0;
    sort(dictionary.begin(), dictionary.end());
    for(i = 0; i < dictionary.size(); i++){
        if(isSubsequence(dictionary[i], s)){
            if(len < dictionary[i].size()){
                len = dictionary[i].size();
                j = i;
            }
        }
    }
    return j != -1 ? dictionary[j] : "";
}

int main(){
    string s = "abpcplea"; vector<string> dictionary = {"apple", "ale", "monkey", "plea"};
    cout << findLongestWord(s, dictionary) << endl;
    return 0;
}
